// 事件处理
export function useEventHandling({ uid, sessionId, controlId, sessionStitchMode, sendWebSocketMessage }) {
  // 提取公共参数
  const createCommonParams = (eventType) => ({
    uid: uid.value,
    session_id: sessionId.value,
    cid: controlId.value,
    type: eventType
  })

  // 创建自由视角参数
  const createFreeviewParams = (eventType, { startX, startY, playerWidth, playerHeight }) => ({
    ...createCommonParams(eventType),
    win_x: startX,
    win_y: startY,
    win_width: playerWidth,
    win_height: playerHeight,
    pos_x: 1
  })

  // 创建全景拼接/融合视角参数
  const createStitchParams = (
    eventType,
    { zoom, xOffset, yOffset, startX, endX, startY, endY, playerWidth, playerHeight }
  ) => ({
    ...createCommonParams(eventType),
    zoom,
    x_offset: xOffset,
    y_offset: yOffset,
    s_x: startX,
    e_x: endX,
    s_y: startY,
    e_y: endY,
    win_width: playerWidth,
    win_height: playerHeight
  })

  // 处理事件并发送到服务器
  const handleEvent = (eventType, additionalParams) => {
    const eventTypeMap = {
      wheel: 'whell'
    }
    eventType = eventTypeMap[eventType] || eventType

    const isFreeView = sessionStitchMode.value === 'freeview'
    const params = isFreeView
      ? createFreeviewParams(eventType, additionalParams)
      : createStitchParams(eventType, additionalParams)

    sendWebSocketMessage(params)
  }

  return {
    handleEvent
  }
}
